import os
import shutil
from cx_Freeze import setup, Executable

bin_excludes =[
      "d3dcompiler_47.dll",
      "libEGL.dll",
      "opengl32sw.dll",
      "Qt3DAnimation.pyd",
      "Qt3DCore.pyd",
      "Qt3DExtras.pyd",
      "Qt3DInput.pyd",
      "Qt3DLogic.pyd",
      "Qt3DRender.pyd",
      "Qt53DAnimation.dll",
      "Qt53DCore.dll",
      "Qt53DExtras.dll",
      "Qt53DInput.dll",
      "Qt53DLogic.dll",
      "Qt53DRender.dll",
      "Qt5Charts.dll",
      "Qt5Concurrent.dll",
      "Qt5DataVisualization.dll",
      "Qt5DBus.dll",
      "Qt5Gamepad.dll",
      "Qt5Help.dll",
      "Qt5Location.dll",
      "Qt5Multimedia.dll",
      "Qt5MultimediaWidgets.dll",
      "Qt5OpenGL.dll",
      "Qt5Pdf.dll",
      "Qt5Positioning.dll",
      "Qt5PositioningQuick.dll",
      "Qt5QmlModels.dll",
      "Qt5Quick.dll",
      "Qt5QuickControls2.dll",
      "Qt5QuickTemplates2.dll",
      "Qt5QuickWidgets.dll",
      "Qt5RemoteObjects.dll",
      "Qt5Script.dll",
      "Qt5ScriptTools.dll",
      "Qt5Scxml.dll",
      "Qt5Sensors.dll",
      "Qt5SerialPort.dll",
      "Qt5Sql.dll",
      "Qt5Svg.dll",
      "Qt5Test.dll",
      "Qt5TextToSpeech.dll",
      "Qt5WebChannel.dll",
      "Qt5WebEngine.dll",
      "Qt5WebEngineCore.dll",
      "Qt5WebEngineWidgets.dll",
      "Qt5WebSockets.dll",
      "Qt5WebView.dll",
      "Qt5WinExtras.dll",
      "Qt5XmlPatterns.dll",
      "QtAxContainer.pyd",
      "QtCharts.pyd",
      "QtConcurrent.pyd",
      "QtDataVisualization.pyd",
      "QtHelp.pyd",
      "QtLocation.pyd",
      "QtMultimedia.pyd",
      "QtMultimediaWidgets.pyd",
      "QtOpenGL.pyd",
      "QtOpenGLFunctions.pyd",
      "QtPositioning.pyd",
      "QtQuick.pyd",
      "QtQuickControls2.pyd",
      "QtQuickWidgets.pyd",
      "QtRemoteObjects.pyd",
      "QtScript.pyd",
      "QtScriptTools.pyd",
      "QtScxml.pyd",
      "QtSensors.pyd",
      "QtSerialPort.pyd",
      "QtSql.pyd",
      "QtSvg.pyd",
      "QtTest.pyd",
      "QtTextToSpeech.pyd",
      "QtWebEngine.pyd",
      "QtWebEngineCore.pyd",
      "QtWebEngineProcess.exe",
      "QtWebEngineWidgets.pyd",
      "QtAxContainer.pyi",
      "QtCharts.pyi",
      "QtConcurrent.pyi",
      "QtDataVisualization.pyi",
      "QtHelp.pyi",
      "QtLocation.pyi",
      "QtMultimedia.pyi",
      "QtMultimediaWidgets.pyi",
      "QtOpenGL.pyi",
      "QtOpenGLFunctions.pyi",
      "QtPositioning.pyi",
      "QtQuick.pyi",
      "QtQuickControls2.pyi",
      "QtQuickWidgets.pyi",
      "QtRemoteObjects.pyi",
      "QtScript.pyi",
      "QtScriptTools.pyi",
      "QtScxml.pyi",
      "QtSensors.pyi",
      "QtSerialPort.pyi",
      "QtSql.pyi",
      "QtSvg.pyi",
      "QtTest.pyi",
      "QtTextToSpeech.pyi",
      "QtWebEngine.pyi",
      "QtWebEngineCore.pyi",
      "QtWebSockets.pyd",
      "QtWinExtras.pyd",
      "QtXmlPatterns.pyd",
      "sqlite3.dll",
      "libGLESv2.dll",
      "libclang-13.dll",
      "libcrypto-3-x64.dll",
      "libxml2.dll",
      "designer.exe",
      "pyside2.abi3.lib",
      "Qt5Bluetooth.dll",
      "Qt5Bodymovin.dll",
      "Qt5Designer.dll",
      "Qt5DesignerComponents.dll",
      "Qt5NetworkAuth.dll",
      "Qt5Nfc.dll",
      "Qt5PdfWidgets.dll",
      "Qt5Purchasing.dll",
      "Qt5QmlWorkerScript.dll",
      "Qt5SerialBus.dll",
      "Qt5VirtualKeyboard.dll",
      "QtXmlPatterns.pyi",
      "shiboken2.abi3.lib",
]


setup(
      name = "Serialer",
      version = "0.1",
      options={
            "build_exe": {
                  "optimize": 1,
                  "replace_paths": [("src", "")],
                  "no_compress": False,
                  "include_files": [
                        ("src\\app.ui", "app.ui")
                  ],
                  "packages": ['numpy', 'PySide2', 'pyqtgraph', 'serial', 'src.device'],
                  "excludes": [
                        "tkinter", "PyInstaller", "py2exe" ,"cx_Freeze", "sqlite3", "setuptools", "email", "mkl",
                        "numpy_mkl", "asyncio", "concurrent", "curses", "distutils", "html", "http", "lib2to3",
                        "logging", "pkg_resources", "pydoc_data", "test", "unittest", "xml", "xmlrpc", "pynsist"
                  ],
                  "bin_excludes": bin_excludes
            }
      },
      description = "串口调试",
      executables = [Executable(
            "src\\serialer.py", 
            base='Win32GUI'
      )]
)

BASEDIR = os.path.dirname(os.path.abspath(__file__))

shutil.copy(f"{BASEDIR}\\build\\exe.win32-3.10\\lib\\src\\device.pyc", f"{BASEDIR}\\build\\exe.win32-3.10\\lib\\")

for each in os.listdir("build\\exe.win32-3.10\\lib\\PySide2\\plugins"):
      if each == 'platforms':
            for every in os.listdir("build\\exe.win32-3.10\\lib\\PySide2\\plugins\\platforms"):
                  if every == "qwindows.dll": continue
                  os.remove(f"{BASEDIR}\\build\\exe.win32-3.10\\lib\\PySide2\\plugins\\platforms\\{every}")
            continue
      shutil.rmtree(f"{BASEDIR}\\build\\exe.win32-3.10\\lib\\PySide2\\plugins\\{each}")

for each in os.listdir("build\\exe.win32-3.10\\lib\\PySide2\\"):
      if os.path.isdir(f"build\\exe.win32-3.10\\lib\\PySide2\\{each}") and each != 'plugins':
            shutil.rmtree(f"{BASEDIR}\\build\\exe.win32-3.10\\lib\\PySide2\\{each}")
      if each[-3:] == 'exe' or each[:5] in ['QtWeb', 'QtSvg', 'QtSql']:
            os.remove(f"{BASEDIR}\\build\\exe.win32-3.10\\lib\\PySide2\\{each}")
      if 'Quick' in each or each[:4] == 'Qt3D':
            os.remove(f"{BASEDIR}\\build\\exe.win32-3.10\\lib\\PySide2\\{each}")

for each in os.listdir("build\\exe.win32-3.10\\lib\\"):
      if each == 'numpy_mkl':
            shutil.rmtree(f"{BASEDIR}\\build\\exe.win32-3.10\\lib\\{each}")

shutil.rmtree(f"{BASEDIR}\\build\\exe.win32-3.10\\lib\\src")

shutil.rmtree(f"{BASEDIR}\\build\\exe.win32-3.10\\lib\\shiboken2\\docs")
